package com.fhao.forum.controller;

import com.fhao.forum.common.AppResult;
import com.fhao.forum.common.ResultCode;
import com.fhao.forum.config.AppConfig;
import com.fhao.forum.model.Article;
import com.fhao.forum.model.Board;
import com.fhao.forum.model.User;
import com.fhao.forum.services.IArticleService;
import com.fhao.forum.services.IBoardService;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@ApiResponses
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private IBoardService boardService;

    @Resource
    private IArticleService articleService;

    @ApiResponse(description = "发布新帖子")
    @PostMapping("/create")
    public AppResult<Article> create(HttpServletRequest request,
                                     @RequestParam("boardId") @NonNull Long boardId,
                                     @RequestParam("title") @NonNull String title,
                                     @RequestParam("content") @NonNull String content) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1) {
            //用户已禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //版块校验
        Board board = boardService.selectById(boardId);
        if(board == null || board.getDeleteState() == 1 || board.getState() == 1) {
            //打印日志
            log.warn("ArticleController create方法"+ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }
        Article article = new Article();
        article.setUserId(user.getId());
        article.setContent(content);
        article.setTitle(title);
        article.setBoardId(boardId);
        //调用articleService中的create方法
        articleService.create(article);
        log.info("ArticleController create 成功");
        return AppResult.success();
    }

    @ApiResponse(description = "获取帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId (@RequestParam(value = "boardId",required = false) Long boardId) {
        //定义一个结果集
        List<Article> articles;

        if(boardId == null) {
            //查询所有
            articles = articleService.selectAll();
        }else {
            articles = articleService.selectAllByBoardId(boardId);
        }
        //结果是否为空
        if(articles == null) {
            //如果结果集为空,那么创建一个空集合.
            articles = new ArrayList<>();
        }

        return AppResult.success(articles);
    }

    @ApiResponse(description = "根据帖子id获取帖子详情")
    @GetMapping("/details")            // ↓ 发送请求的参数重命名
    public AppResult<Article> getDetails(HttpServletRequest request,
                                         @RequestParam("id") @NonNull Long id) {
        //从session中获取当前用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service层,拿到帖子详情
        Article article = articleService.selectDetailById(id);
        //校验帖子详情
        if(article == null) {
            log.warn("ArticleController getDetails, "+ResultCode.FAILED_NOT_EXISTS);
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        //判断当前登录用户是否为帖子作者,并设置参数
        if(user.getId() == article.getUserId()) {
            article.setOwn(true);
        }
        //返回结果
        return AppResult.success(article);
    }

    @ApiResponse(description = "编辑帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @RequestParam("id") @NonNull Long id,
                            @RequestParam("title") @NonNull String title,
                            @RequestParam("content") @NonNull String content) {
        log.info("编辑帖子");
        HttpSession session = request.getSession(false);
        //拿到当前登录用户
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断当前登录用户是否被禁言
        if(user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //拿到帖子
        Article article = articleService.selectById(id);
        //校验帖子
        if(article == null) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断该文章作者是否为当前登录用户
        if(article.getUserId() != user.getId()) {
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //判断帖子的状态-是否已经归档
        if(article.getState() == 1 || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //调用service层,编辑帖子
        articleService.modify(id,title,content);
        log.info("ArticleController modify方法, 帖子更新成功");
        return AppResult.success();
    }


    @ApiResponse(description = "点赞")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,
                              @RequestParam("id") @NonNull Long id) {
        //查看当前用是否被禁言
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //判断帖子是否存在
        Article article = articleService.selectById(id);
        if(article == null || article.getDeleteState() == 1) {
            log.warn("ArticleController thumbsUp方法, "+ResultCode.FAILED_BOARD_NOT_EXISTS);
            return AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        if(article.getState() == 1) {
            log.warn("ArticleController thumbsUp方法, "+ResultCode.FAILED_ARTICLE_BANNED);
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //调用Service
        articleService.thumbsUpById(id);
        //返回结果
        return AppResult.success();
    }
    @ApiResponse(description = "根据帖子id删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,
                                @RequestParam("id") @NonNull Long id) {
        log.info("ArticleController deleteById 刪除帖子");
        //校验用户状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1) {
            //用户被禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子详情
        Article article = articleService.selectById(id);
        if(article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断当前登录用户是否为帖子作者
        if(user.getId() != article.getUserId()) {
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        //删除帖子
        articleService.deleteById(id);
        //操作成功
        return AppResult.success();
    }

    @ApiResponse(description = "获取用户的帖子列表")
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,
                                                   @RequestParam(value = "userId",required = false) Long userId) {
        //如果userId为空,那么获取当前登录用户的id
        if(userId == null) {
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute(AppConfig.USER_SESSION);
            userId = user.getId();
        }
        //调用service
        List<Article> articles = articleService.selectByUserId(userId);
        if(articles == null) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        return AppResult.success(articles);
    }
}




















